import { JavaTypeConst } from './constants';

export type IRuntimeVariableType =
  | number
  | boolean
  | string
  | RuntimeVariable[];

/**
 * 运行时变量
 *
 * @author chitanda
 * @date 2022-12-01 15:12:33
 * @export
 * @class RuntimeVariable
 */
export class RuntimeVariable {
  private _parent: RuntimeVariable | null = null;

  get parent() {
    return this._parent!;
  }

  set parent(val: RuntimeVariable) {
    this._parent = val;
  }

  get value(): IRuntimeVariableType {
    return this._value;
  }

  set value(value: IRuntimeVariableType) {
    this._value = value;
  }

  get type() {
    return this._type;
  }

  private _reference: number = -1;

  get reference(): number {
    return this._reference;
  }

  set reference(val: number) {
    this._reference = val;
  }

  /**
   * 是否为基本数据类型
   *
   * @author chitanda
   * @date 2022-12-01 22:12:29
   * @type {boolean}
   */
  readonly isBasicDataType: boolean;

  constructor(
    readonly name: string,
    private _value: IRuntimeVariableType,
    private _type: string,
  ) {
    switch (_type) {
      case JavaTypeConst.Byte:
      case JavaTypeConst.Char:
      case JavaTypeConst.String:
      case JavaTypeConst.Boolean:
      case JavaTypeConst.Int:
      case JavaTypeConst.Short:
      case JavaTypeConst.Double:
      case JavaTypeConst.Float:
      case JavaTypeConst.Long:
        this.isBasicDataType = true;
        break;
      default:
        this.isBasicDataType = false;
    }
  }

  /**
   * 计算参数路径
   *
   * @description 参数示例: xxx.yyy.zzz
   * @author chitanda
   * @date 2022-12-02 00:12:48
   * @readonly
   * @type {string}
   */
  get parameterPath(): string {
    const path = this.parent?.parameterPath;
    if (path) {
      return `${path}.${this.name}`;
    }
    return this.name;
  }
}
